
import 'package:flutter/material.dart';

/*
虚线分割线

 */
class XDividerDashedLine extends StatelessWidget{

  final double strokeWidth;
  final Color color;
  final double dashWidth;
  final double dashSpace;

  const XDividerDashedLine(
      {super.key,
     this.strokeWidth=0.5,
     this.color= const Color(0xffbdbdbd),
     this.dashWidth=3,
     this.dashSpace=6,
  });

  @override
  Widget build(BuildContext context) {
    return CustomPaint(
      painter: DashedBorderPainter(
        strokeWidth: strokeWidth,
        color: color,
        dashWidth: dashWidth,
        dashSpace: dashSpace,
      ),
    );
  }


}

class DashedBorderPainter extends CustomPainter {
  final double strokeWidth;
  final Color color;
  final double dashWidth;
  final double dashSpace;

  DashedBorderPainter({
    required this.strokeWidth,
    required this.color,
    required this.dashWidth,
    required this.dashSpace,
  });

  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = color
      ..strokeWidth = strokeWidth
      ..style = PaintingStyle.stroke;

    //
    double tmp = 0;
    double length = size.width;
    double delta;

    while (tmp < length) {
      delta = (tmp + dashWidth < length) ? dashWidth : length - tmp;
      canvas.drawLine(Offset(tmp, 0), Offset(tmp + delta, 0), paint);
      if (tmp + delta >= length) {
        break;
      }

      tmp = (tmp + dashWidth + dashSpace < length)
          ? (tmp + dashWidth + dashSpace)
          : (length);
    }

  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return false;
  }
}